package com.ztgf.sys.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.sys.models.bo.SysRoleMenuRelationBO;
import com.ztgf.sys.models.dto.SysRoleMenuRelationDTO;
import com.ztgf.sys.models.service.ISysRoleMenuRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 角色菜单关系表控制器
 *
 * @author YUE
 * @Date 2020-05-27 16:37:54
 */

@RestController
@RequestMapping("/api/sysRoleMenuRelation")
@Slf4j
@Api(value = "SysRoleMenuRelationApiController",tags = "角色菜单关系表Api接口")
public class SysRoleMenuRelationApiController {

    @DubboReference
    private ISysRoleMenuRelationService sysRoleMenuRelationService;


    @PostMapping("/updateShopRoleMenu")
    @ApiOperation(value = "更新或者添加权限",notes = "更新或者添加权限")
    public Result<SysRoleMenuRelationDTO> updateShopRoleMenu(SysRoleMenuRelationBO sysRoleMenuRelationBO){
        boolean b = false;
        try {
            b = sysRoleMenuRelationService.updateShopRoleMenu(sysRoleMenuRelationBO);
        } catch (Exception e) {
            return new Result<>(CodeEnum.FAIL_100001);
        }
        if(!b){
            return new Result<>(CodeEnum.FAIL_100008);
        }
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<SysRoleMenuRelationDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        SysRoleMenuRelationDTO dto = sysRoleMenuRelationService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "角色菜单关系表列表",notes = "角色菜单关系表列表")
    @PostMapping("/queryPageList")
    public Result<Page<SysRoleMenuRelationDTO>> queryPageList(SysRoleMenuRelationBO bo){
       Wrapper<SysRoleMenuRelationDTO> wrapper = new EntityWrapper<>();
        Page<SysRoleMenuRelationDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = sysRoleMenuRelationService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}
